﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class QuanTri_PhanCongChuyenMon : System.Web.UI.Page
{
    SoLienLacDienTu cls = new SoLienLacDienTu();
    SoLienLacDataContext db = new SoLienLacDataContext();
    // string MaLop;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
        }
        else
        {
            // MaLop=Request.QueryString["MaKhoi"];
            if (!IsPostBack)
            {
                cls.LoadddlKhoiHoc(ddlKhoi);
                cls.LoadNamHoc(dllNamHoc);
                LoadgrvPhanCong();
                //Loadl(); 
                LoadgrvLop();
                btnLuu.Enabled = false;
                btnXoa.Enabled = false;
                // btnXuat.Enabled = false;
                ckbTaCa.Enabled = false;
            }
        }
    }
    void LoadgrvPhanCong()
    {
        var c = from p in db.MonHocs select new { p.TenMon, p.MaMon };
        grvPhanCong.DataSource = c;
        grvPhanCong.DataBind();
    }
    void LoadgrvLop()
    {
        grvdsLop.DataSource = db.LoadLopTheoNamKhoi(int.Parse(ddlKhoi.SelectedValue.ToString()), int.Parse(dllNamHoc.SelectedValue.ToString()));
        grvdsLop.DataBind();
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        if (lblMa.Text != "")
        {
            for (int i = 0; i < grvPhanCong.Rows.Count; i++)
            {

                Label Ma = (Label)grvPhanCong.Rows[i].Cells[1].FindControl("lblMaMon");
                DropDownList GV = (DropDownList)grvPhanCong.Rows[i].Cells[2].FindControl("ddlGiaoVien");
                CheckBox ck = (CheckBox)grvPhanCong.Rows[i].Cells[0].FindControl("ckItem");
                var c = from p in db.GiaoVien_Lop_Mons where p.MaLop == int.Parse(lblMa.Text) && p.MaNam == int.Parse(dllNamHoc.SelectedValue.ToString()) select p.MaLop;
                if (ck.Checked == true)
                {
                    if (c.Count() == 0)
                    {
                        GiaoVien_Lop_Mon gm = new GiaoVien_Lop_Mon();
                        gm.MaLop = int.Parse(lblMa.Text);
                        gm.MaMon = int.Parse(Ma.Text);
                        gm.MaNam = int.Parse(dllNamHoc.SelectedValue.ToString());
                        gm.MaCanBoGV = GV.SelectedValue.ToString();
                        gm.TrangThaiGiangDay = true;
                        db.GiaoVien_Lop_Mons.InsertOnSubmit(gm);

                    }
                    if (c.Count() != 0)
                    {
                        GiaoVien_Lop_Mon gm = db.GiaoVien_Lop_Mons.SingleOrDefault(p => p.MaLop == int.Parse(lblMa.Text) && p.MaNam == int.Parse(dllNamHoc.SelectedValue.ToString()));
                        gm.MaMon = int.Parse(Ma.Text);
                        gm.MaNam = int.Parse(dllNamHoc.SelectedValue.ToString());
                        gm.MaCanBoGV = GV.SelectedValue.ToString();
                        gm.TrangThaiGiangDay = true;
                        // db.SubmitChanges();
                    }
                }
            }
            //LoadgrvPhanCong();
            db.SubmitChanges();
           // btnXuat.Enabled = true;
            btnXoa.Enabled = true;
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Bạn chưa chọn lớp để phân công giảng dạy')", true);
        }
    }
    protected void grvPhanCong_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (grvPhanCong.Rows.Count >= 1)
        {
            for (int i = 0; i < grvPhanCong.Rows.Count; i++)
            {
                Label MaMon = (Label)grvPhanCong.Rows[i].Cells[0].FindControl("lblMaMon");
                DropDownList ddlGV = (DropDownList)grvPhanCong.Rows[i].Cells[2].FindControl("ddlGiaoVien");
                var c = from p in db.ToBoMons where p.MaMon == int.Parse(MaMon.Text) select new { p.MaCanBoGV, p.CanBoGiaoVien.TenCanBo};
                ddlGV.DataTextField = "TenCanBo";
                ddlGV.DataValueField = "MaCanBoGV";
                ddlGV.DataSource = c;
                ddlGV.DataBind();
            }
        }
    }
    void LoadCanBo()
    {

        LoadCanBo();
    }
    protected void ddlKhoi_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadgrvLop();
    }
    protected void grvdsLop_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            GridViewRow row = grvdsLop.SelectedRow;
            Label ma = (Label)row.FindControl("lblMaLop");
            var c = from i in
                        (from p in db.Lop_HocSinhs where p.MaLop == int.Parse(ma.Text) select new { p.MaLop, p.LopHoc.TenLop })
                    group i by new { i.MaLop, i.TenLop } into g
                    select new { g.Key.MaLop, g.Key.TenLop };
            lblMa.Text = c.FirstOrDefault().MaLop.ToString();
            lblTenLop.Text = c.FirstOrDefault().TenLop;
            ckbTaCa.Enabled = true;
            for (int i = 0; i < grvPhanCong.Rows.Count; i++)
            {

                DropDownList dr = (DropDownList)grvPhanCong.Rows[i].Cells[2].FindControl("ddlGiaoVien");
                Label Mon = (Label)grvPhanCong.Rows[i].Cells[1].FindControl("lblMaMon");
                var k = from p in db.GiaoVien_Lop_Mons where p.MaLop == int.Parse(ma.Text) && p.MaNam == int.Parse(dllNamHoc.SelectedValue.ToString()) && p.MaMon == int.Parse(Mon.Text) select p.CanBoGiaoVien.TenCanBo;
                if (k.Count() > 0)
                {
                    dr.SelectedItem.Text = k.First().ToString();
                    btnLuu.Enabled = false;
                    btnXoa.Enabled = true;
                }
                else
                    if (k.Count() == 0)
                    {
                        if (grvPhanCong.Rows.Count >= 1)
                        {
                            for (int j = 0; j < grvPhanCong.Rows.Count; j++)
                            {
                                Label MaMon = (Label)grvPhanCong.Rows[j].Cells[0].FindControl("lblMaMon");
                                DropDownList ddlGV = (DropDownList)grvPhanCong.Rows[j].Cells[2].FindControl("ddlGiaoVien");
                                var c1 = from p in db.ToBoMons where p.MaMon == int.Parse(MaMon.Text) select new { p.MaCanBoGV, p.CanBoGiaoVien.TenCanBo };
                                ddlGV.DataTextField = "TenCanBo";
                                ddlGV.DataValueField = "MaCanBoGV";
                                ddlGV.DataSource = c1;
                                ddlGV.DataBind();
                            }
                        }
                        btnLuu.Enabled = true;
                    }
            }
        }
        catch(Exception ex)
        { ex.ToString(); }
    }
    protected void ckbTaCa_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < grvPhanCong.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)grvPhanCong.Rows[i].Cells[0].FindControl("ckItem");
            if (ckbTaCa.Checked == true)
            {


                ck.Checked = true;
            }

            else
            {
                ck.Checked = false;
            }
        }
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < grvPhanCong.Rows.Count; i++)
        {
            Label Mon = (Label)grvPhanCong.Rows[i].Cells[1].FindControl("lblMaMon");
            DropDownList dr = (DropDownList)grvPhanCong.Rows[i].Cells[2].FindControl("ddlGiaoVien");
            string ma = dr.SelectedValue.ToString();
           // Response.Write(ma);
            CheckBox ck = (CheckBox)grvPhanCong.Rows[i].Cells[0].FindControl("ckItem");
            if(ck.Checked==true)
            {
                GiaoVien_Lop_Mon gm = db.GiaoVien_Lop_Mons.SingleOrDefault
                    (p => p.MaLop == int.Parse(lblMa.Text)
                    && p.MaMon == int.Parse(Mon.Text) && p.MaNam
                    == int.Parse(dllNamHoc.SelectedValue.ToString())
                    && p.MaCanBoGV == dr.SelectedValue.ToString()
                    &&p.MaLop==int.Parse(lblMa.Text));
                gm.TrangThaiGiangDay = false;
            }
            
        }  
        db.SubmitChanges();
       // LoadgrvPhanCong();
    }
}
